﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://www.codeplex.com/EmlenMud
using System.Text;
using System.Runtime.Serialization;
using System;
using System.Threading;
using System.ComponentModel;
using System.Collections;
using System.Collections.Generic;
using BrainTechLLC.ThreadSafeObjects;
using WordsMatching;
using System.Xml.Serialization;
using System.Collections.Specialized;
#if USE_HYPER
using Hyper.ComponentModel;
#endif

namespace BrainTechLLC
{
    [Serializable]
    [DataContract]
#if NO_SILVERLIGHT
    //[TypeDescriptionProvider(typeof(HyperTypeDescriptionProvider))]
#endif
    public class BaseSavable : BaseAssociatedId, ICanSaveAndRestore
    {
        [NonSerialized, XmlIgnore]
        public DateTime _lastChanged = DateTime.Now;

        [NonSerialized, XmlIgnore]
        public DateTime _lastSaved = DateTime.MinValue;

        [Category("Change and Persistence")]
        [XmlIgnore]
        [DoNotWriteBack]
        public virtual DateTime LastChanged { get { return _lastChanged; } set { _lastChanged = value; } }

        [Category("Change and Persistence")]
        [XmlIgnore]
        [DoNotWriteBack]
        public virtual DateTime LastSaved { get { return _lastSaved; } set { _lastSaved = value; } }

        public virtual void MarkChanged() { _lastChanged = DateTime.Now; }
        public virtual void MarkSaved() { _lastSaved = DateTime.Now; }
        public virtual void MarkRestored() { MarkSaved(); }

        [NonSerialized, XmlIgnore]
        private static readonly Dictionary<Type, bool> d = new Dictionary<Type, bool>();

        public virtual void Save(List<object> arguments)
        {
            if (_lastSaved < _lastChanged || true)
            {
                //#if NO_SILVERLIGHT
                //            string fileName = "C:\\gamething_" + AssociatedID.ToString();
                //            byte[] bytes = this.SerializeToCompressedBinary();
                //            File.WriteAllBytes(fileName, bytes);
                //#endif
                _lastSaved = DateTime.Now;
            }
        }

        public bool Restore<T>(List<object> arguments, out T restored) where T : class
        {
            throw new NotImplementedException();
        }

        public virtual void PerformPostRestoreSetup()
        {

        }
    }
}